﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang.HeThong
{
	public partial class F130_Phan_quyen_cho_nhom : Form
	{
		public F130_Phan_quyen_cho_nhom()
		{
			InitializeComponent();
			set_define_events();
		}

		#region Members
		BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
		#endregion

		#region Private Methods
		private void set_define_events()
		{
			this.Load += F130_Phan_quyen_cho_nhom_Load;
			this.m_btn_them_mot_chuc_nang.Click += m_btn_them_mot_chuc_nang_Click;
			this.m_btn_them_tat_ca_chuc_nang.Click += m_btn_them_tat_ca_chuc_nang_Click;
			this.m_btn_xoa_mot_chuc_nang.Click += m_btn_xoa_mot_chuc_nang_Click;
			this.m_btn_xoa_tat_ca_chuc_nang.Click += m_btn_xoa_tat_ca_chuc_nang_Click;
			this.m_cbo_nhom_nguoi_dung.SelectedIndexChanged += m_cbo_nhom_nguoi_dung_SelectedIndexChanged;
		}



		private void load_data_to_cbo_nhom_nguoi_dung()
		{
			var v_lst_nhom_nguoi_dung = db.HT_NHOM_NGUOI_DUNG
				.Select(x => new
				{
					ID = x.ID,
					TEN_NHOM = x.TEN_NHOM
				})
				.OrderBy(x => x.TEN_NHOM)
				.ToList();
			m_cbo_nhom_nguoi_dung.DisplayMember = "TEN_NHOM";
			m_cbo_nhom_nguoi_dung.ValueMember = "ID";
			m_cbo_nhom_nguoi_dung.DataSource = v_lst_nhom_nguoi_dung;


		}
		private void load_data_to_cbo_chuc_nang_da_co()
		{
			decimal v_dc_id_nhom_nguoi_dung = Convert.ToDecimal(m_cbo_nhom_nguoi_dung.SelectedValue);
			var v_lst_chuc_nang_da_co = db.HT_PHAN_QUYEN_NHOM
				.Where(x => x.ID_NHOM_NGUOI_DUNG == v_dc_id_nhom_nguoi_dung)
				.Select(x => new
				{
					ID = x.HT_CHUC_NANG.ID,
					TEN_CHUC_NANG = x.HT_CHUC_NANG.TEN_CHUC_NANG
				})
				.ToList();
			m_lsb_chuc_nang_da_co.DataSource = v_lst_chuc_nang_da_co;
			m_lsb_chuc_nang_da_co.ValueMember = "ID";
			m_lsb_chuc_nang_da_co.DisplayMember = "TEN_CHUC_NANG";
		}
		private void load_data_to_cbo_chuc_nang_chua_co()
		{
			decimal v_dc_id_nhom_nguoi_dung = Convert.ToDecimal(m_cbo_nhom_nguoi_dung.SelectedValue);
			var v_lst_chuc_nang_chua_co = db.HT_CHUC_NANG
				.Where(x => x.HT_PHAN_QUYEN_NHOM.Count(y=>y.ID_NHOM_NGUOI_DUNG==v_dc_id_nhom_nguoi_dung
														&&y.ID_CHUC_NANG==x.ID)==0)
				.Select(x => new
				{
					ID = x.ID,
					TEN_CHUC_NANG = x.TEN_CHUC_NANG
				})
				.ToList();
			m_lsb_chuc_nang_chua_co.DataSource = v_lst_chuc_nang_chua_co;
			m_lsb_chuc_nang_chua_co.ValueMember = "ID";
			m_lsb_chuc_nang_chua_co.DisplayMember = "TEN_CHUC_NANG";
		}
		#endregion

		#region Events

		void m_btn_xoa_tat_ca_chuc_nang_Click(object sender, EventArgs e)
		{
			try
			{
				decimal v_dc_id_nhom_nguoi_dung = Convert.ToDecimal(m_cbo_nhom_nguoi_dung.SelectedValue);
				var v_lst_phan_quyen_cho_nhom = db.HT_PHAN_QUYEN_NHOM
											.Where(x => x.ID_NHOM_NGUOI_DUNG == v_dc_id_nhom_nguoi_dung)
											.ToList();
				//Xoa phan quyen 
				for (int i = 0; i < v_lst_phan_quyen_cho_nhom.Count; i++)
				{
					db.HT_PHAN_QUYEN_NHOM.Remove(v_lst_phan_quyen_cho_nhom[i]);
					db.SaveChanges();
				}

				//reload 2 list box
				load_data_to_cbo_chuc_nang_da_co();
				load_data_to_cbo_chuc_nang_chua_co();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void m_btn_xoa_mot_chuc_nang_Click(object sender, EventArgs e)
		{
			try
			{
				decimal v_dc_id_chuc_nang = Convert.ToDecimal(m_lsb_chuc_nang_da_co.SelectedValue);
				decimal v_dc_id_nhom_nguoi_dung = Convert.ToDecimal(m_cbo_nhom_nguoi_dung.SelectedValue);
				HT_PHAN_QUYEN_NHOM v_ht_phan_quyen = db.HT_PHAN_QUYEN_NHOM
					.FirstOrDefault(x => x.ID_CHUC_NANG == v_dc_id_chuc_nang
										&& x.ID_NHOM_NGUOI_DUNG == x.ID_NHOM_NGUOI_DUNG);
				if (v_ht_phan_quyen != null)
				{
					db.HT_PHAN_QUYEN_NHOM.Remove(v_ht_phan_quyen);
					db.SaveChanges();
					//reload 2 list box
					load_data_to_cbo_chuc_nang_da_co();
					load_data_to_cbo_chuc_nang_chua_co();

				}

			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void m_btn_them_tat_ca_chuc_nang_Click(object sender, EventArgs e)
		{
			try
			{
				decimal v_dc_id_nhom_nguoi_dung = Convert.ToDecimal(m_cbo_nhom_nguoi_dung.SelectedValue);
				//list chuc nang chua co
				var v_lst_chuc_nang_chua_co = db.HT_CHUC_NANG
							.Where(x => x.HT_PHAN_QUYEN_NHOM.Count(y => y.ID_NHOM_NGUOI_DUNG == v_dc_id_nhom_nguoi_dung
															&& y.ID_CHUC_NANG == x.ID) == 0)
						   .ToList();
				//Xoa phan quyen 
				for (int i = 0; i < v_lst_chuc_nang_chua_co.Count; i++)
				{
					HT_PHAN_QUYEN_NHOM v_ht_phan_quyen = new HT_PHAN_QUYEN_NHOM();
					v_ht_phan_quyen.ID_NHOM_NGUOI_DUNG = v_dc_id_nhom_nguoi_dung;
					v_ht_phan_quyen.ID_CHUC_NANG = v_lst_chuc_nang_chua_co[i].ID;
					db.HT_PHAN_QUYEN_NHOM.Add(v_ht_phan_quyen);
					db.SaveChanges();
				}

				//reload 2 list box
				load_data_to_cbo_chuc_nang_da_co();
				load_data_to_cbo_chuc_nang_chua_co();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void m_btn_them_mot_chuc_nang_Click(object sender, EventArgs e)
		{
			try
			{
				decimal v_dc_id_chuc_nang = Convert.ToDecimal(m_lsb_chuc_nang_chua_co.SelectedValue);
				decimal v_dc_id_nhom_nguoi_dung = Convert.ToDecimal(m_cbo_nhom_nguoi_dung.SelectedValue);
				HT_PHAN_QUYEN_NHOM v_ht_phan_quyen = new HT_PHAN_QUYEN_NHOM();
				v_ht_phan_quyen.ID_NHOM_NGUOI_DUNG = v_dc_id_nhom_nguoi_dung;
				v_ht_phan_quyen.ID_CHUC_NANG = v_dc_id_chuc_nang;
				db.HT_PHAN_QUYEN_NHOM.Add(v_ht_phan_quyen);
				db.SaveChanges();
				//reload 2 list box
				load_data_to_cbo_chuc_nang_da_co();
				load_data_to_cbo_chuc_nang_chua_co();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}



		void F130_Phan_quyen_cho_nhom_Load(object sender, EventArgs e)
		{
			try
			{
				load_data_to_cbo_nhom_nguoi_dung();
				load_data_to_cbo_chuc_nang_chua_co();
				load_data_to_cbo_chuc_nang_da_co();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}
		void m_cbo_nhom_nguoi_dung_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				load_data_to_cbo_chuc_nang_chua_co();
				load_data_to_cbo_chuc_nang_da_co();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
